<?php

namespace App\Http\Controllers\Home;

use App\Models\Activitys;
use App\Models\Ads;
use App\Models\Areas;
use App\Models\Categorys;
use App\Models\Configs;
use App\Models\Demands;
use App\Models\Goods;
use App\Models\Messages;
use App\Models\Stores;
use App\Models\Users;
use App\Models\WordCategorys;
use App\Models\Words;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    public function index(Request $request)
    {
        $title = Configs::getConfig('base')['site_name'];
        return view('home.index.index',compact('title'));
    }

    /**
     * banner
     * @param Request $request
     * @param 广告位id $adsense_id
     * @return bool
     */
    public function banner(Request $request){
        try{
            $params = [
                'adsense_id' => 1,
                'status' => 1,
            ];
            $lists = Ads::getList($params, ['id','adsense_id','thumb','link','target']);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 分类数据
     * @param Request $request
     * @return bool
     */
    public function categorys(Request $request){
        try{
            $lists = Categorys::apiCascaderList();//获取分类
            $fields = ['id','store_name','logo','service','status'];
            foreach ($lists as $k=>$v){
                //获取分类下的商铺
                $cid = $v['id'];
                $lists[$k]['store'] = Stores:: select($fields)
                    ->where('status',1)
                    ->where('index_recomment',$cid)->orwhere('index_spare',$cid)
                    ->orderBy('index_recomment', 'desc')
                    ->paginate(5);
            }
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 爬楼数据
     * @param Request $request
     * @param 广告位id $adsense_id
     * @return bool
     */
    public function climbs(Request $request){
        try{
            $lists = Categorys::apiCascaderList();//获取分类
            foreach ($lists as $k=>$v){
                //获取分类下的商铺
                $fields = ['id','store_name','user_id','logo','call','view','hot','quality','cate_id'];
                $cid = $v['id'];
                $lists[$k]['store'] = Stores:: where('status',1)->where('status','<>',-1)
                    ->where('index_recomment',$cid)->orwhere('index_spare',$cid)
                    ->orderBy('index_recomment', 'desc')
                    ->limit(4)->get($fields);
                foreach ($lists[$k]['store'] as $kk=>$vv){
                    $lists[$k]['store'][$kk]['view'] = strlen($vv['view'])>=4 ? '999+' : $vv['view'];
                    $lists[$k]['store'][$kk]['call'] = strlen($vv['call'])>=4 ? '999+' : $vv['call'];
                    //获取当前店铺所属用户的区域
                    $area_id = Users::where('id',$vv['user_id'])->where('status','<>',-1)->value('area');
                    $lists[$k]['store'][$kk]['area'] = Areas::where('id',$area_id)->where('status','<>',-1)->value('name');
                    //擅长领域
                    if (!empty($vv['cate_id'])){
                        $cate_id = explode(',',$vv['cate_id']);
                        foreach ($cate_id as $key => $val){
                            $pidTree[] = Categorys::where(['id'=>$val])->first('name');
                        }
                    }else{
                        $pidTree['name'] = [];
                    }
                    $lists[$k]['store'][$kk]['cate_name'] = $pidTree;
                    $pidTree = [];
                }
                //获取分类下的广告
                $params = [
                    'cate_id' => $v['id'],
                    'adsense_id' => 2,
                    'status' => 1,
                ];
                $lists[$k]['adv'] = Ads::getList($params, $fields = ['id','adsense_id','thumb','link','target']);

                //获取首页中间广告
                $maps = [
                    'adsense_id' => 3,
                    'status' => 1,
                ];
                $adv_center = Ads::getList($maps, $fields = ['id','adsense_id','thumb','link','target']);
                if (!empty($adv_center->toArray())){
                    if ($k == 0){
                        $lists[$k]['adv_center'] = $adv_center[0];
                    }elseif ($k == 3){
                        $lists[$k]['adv_center'] = $adv_center[1];
                    }elseif ($k == 6){
                        $lists[$k]['adv_center'] = $adv_center[2];
                    }
                }
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 热门活动-用于首页和活动列表页右侧
     * @param Request $request
     * @return bool
     */
    public function hotact(Request $request){
        try{
            $lists = Activitys::allLists(['hot'=>2,'status'=>1], ['*'], null,3,'number');
            $now_time = date('Y-m-d H:i:s');
            foreach ($lists as $key => $val){
                $lists[$key]['view'] = strlen($val['view'])>=4 ? '999+' : $val['view'];
                $lists[$key]['number'] = strlen($val['number'])>=4 ? '999+' : $val['number'];
                $lists[$key]['start_at'] = trim(@date('Y-m-d', strtotime($val['start_at'])));
                if (($val['limit'] == $val['number']) || ($now_time > $val['enroll_at'] && $now_time < $val['start_at'])){
                    $lists[$key]['act_state'] = '报名结束';
                }elseif ($now_time <= $val['enroll_at']){
                    $lists[$key]['act_state'] = '报名中';
                }elseif ($now_time >= $val['start_at']){
                    $lists[$key]['act_state'] = '已举办';
                }
            }
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 热销商品-用于首页和商品列表页右侧
     * @param Request $request
     * @return bool
     */
    public function hotgoods(Request $request){
        try{
            $fields = ['id','thumb','title','cate_pid','cate_id','content','price','salenum','view','credit'];
            $lists = Goods::allLists(['hot'=>2,'status'=>1], $fields, null, 3);
            foreach ($lists as $key => $val){
                $lists[$key]['salenum'] = strlen($val['salenum'])>=4 ? '999+' : $val['salenum'];
                $lists[$key]['view'] = strlen($val['view'])>=4 ? '999+' : $val['view'];
            }
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 热门文档-用于文档列表页右侧
     * @param Request $request
     * @return bool
     */
    public function hotword(Request $request){
        try{
            $fields = ['id','thumb','title','cate_pid','cate_id','view','download','format'];
            $lists = Words::allLists(['hot'=>2,'status'=>1], $fields, null, 3);
            foreach ($lists as $key => $val){
                $lists[$key]['view'] = strlen($val['view'])>=4 ? '999+' : $val['view'];
                $lists[$key]['download'] = strlen($val['download'])>=4 ? '999+' : $val['download'];
                $lists[$key]['cate_pid'] = WordCategorys::where('id',$val['cate_pid'])->value('name');
                $lists[$key]['cate_id'] = WordCategorys::where('id',$val['cate_id'])->value('name');
            }
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 成交记录
     * @param Request $request
     * @return bool
     */
    public function transaction(Request $request){
        try{
            $lists = Messages::where('type',5)->where('status','<>',-1)->orderBy('created_at', 'desc')->limit(7)->get('title');
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 首页底部数量统计
     * @param Request $request
     * @param strlen 获取字符串长度
     * @param substr_replace 一个字符串插入另一个字符串
     * @return bool
     */
    public function numbers(Request $request){
        try{
            $lists['store'] = Stores::count();
            $lists['user'] = Users::count();
            $lists['word'] = Words::count();
            $lists['demand'] = Demands::count();
            foreach ($lists as $k=>$v){
                if(strlen($v)>=4){
                    $lists[$k] = substr_replace($v, ',', -3, 0);
                }else{
                    $lists[$k] = $v;
                }
            }
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

}